#pragma once

#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

class InetAddr
{
private:
    void GetAddress(std::string* ip, uint16_t* port)
    {
        *ip = inet_ntoa(_addr.sin_addr);    //四字节ip转化为字符串风格ip 并转化为主机序列
        *port = ntohs(_addr.sin_port);      //端口号网络序列转换为主机序列
    }
public:
    InetAddr(struct sockaddr_in addr)
    :_addr(addr)
    {
        GetAddress(&_ip, &_port);
    }
    std::string Ip()
    {
        return _ip;
    }
    uint16_t Port()
    {
        return _port;
    }
    
    sockaddr_in Addr()
    {
        return _addr;
    }
    bool operator==(const InetAddr& other)
    {
        //如果想要一个服务器中可以有多个客户端进行通信 则在判断InetAddr是否相等时需要考虑port
        return _ip == other._ip && _port == other._port;
        //如果想要一个服务器只有一个客户端进行chat，则判断InetAddr相等时不需要考虑port
        //return _ip == other._ip;
    }
private:
    std::string _ip;
    uint16_t _port;
    sockaddr_in _addr;
};